package com.sh.mytool.cv.service.impl;

import java.util.LinkedHashMap;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.sh.common.page.QueryResult;
import com.sh.mytool.cv.dao.BasicDao;
import com.sh.mytool.cv.domain.Column;
import com.sh.mytool.cv.domain.ImageGallery;
import com.sh.mytool.cv.domain.ImageGalleryCover;
import com.sh.mytool.cv.service.ImageGalleryService;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("imageGalleryService")
public class ImageGalleryServiceImpl implements ImageGalleryService {

    @Resource(name="basicDao")
    private BasicDao<ImageGallery> dao;

    public BasicDao<ImageGallery> getDao() {
        return dao;
    }

    public void setDao(BasicDao<ImageGallery> dao) {
        this.dao = dao;
    }

    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
    public QueryResult<ImageGallery> page(String scope, Object[] params, int begin, int max) {
        LinkedHashMap<String, Object> where = new LinkedHashMap<String, Object>();
        LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
        return dao.getScrollData(ImageGallery.class, begin, max, where, orderby);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void insert(ImageGallery imageGallery, ImageGalleryCover cover) throws Exception {
        if(cover != null){
            cover.setImageGallery(imageGallery);
            imageGallery.setImageGalleryCover(cover);
        }
        dao.insert(imageGallery);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void delete(Long id) throws Exception {
        dao.deleteById(ImageGallery.class, id);
    }

    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED, readOnly=true)
    public ImageGallery get(Long id) {
        return dao.findById(ImageGallery.class, id);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void update(ImageGallery imageGallery, ImageGalleryCover cover)throws Exception {
        ImageGallery oldBean = this.get(imageGallery.getId());
        oldBean.setName(imageGallery.getName());
        if(cover != null){
        	ImageGalleryCover oldCover = oldBean.getImageGalleryCover();
        	if(oldCover != null){
        		oldCover.setImageGallery(null);
        	}
            cover.setImageGallery(oldBean);
            oldBean.setImageGalleryCover(cover);
        }
        dao.update(oldBean);
    }
}
